package com.selftool.bo;

import com.selftool.gui.serverframe.MainFrom;
import com.selftool.log.Logger;
import com.selftool.main.Intermediary;
import com.selftool.util.CommonUtil;
import com.selftool.util.HttpConnection;
/**
 * 按钮逻辑实现
 * */
public class FromBO {
    Intermediary inte = null;
    public FromBO (Intermediary inte){
        this.inte = inte;
    }
    private CommonUtil util = CommonUtil.getCommonUtil();
    public void exeUpdateButton(MainFrom from) throws Exception {

        inte.exec("dos",from,null);
    }

    /***
     * 打开SysConfig
     * @param from
     * @throws Exception
     */
    public void exeSysConfigButton(MainFrom from) throws Exception {
        boolean isWindows = util.isWindows();
        String command = "";
        if(isWindows){
            command = "call "+CommonUtil.getParameter("homepath")+"\\bin\\sysConfig.bat";
        }else{
            command = "/"+CommonUtil.getParameter("homepath")+"/bin/sysConfig.sh";
        }
        Logger.write("start:"+command);
        inte.exec("dos",from,command);
    }

    /***
     * 执行重启指令
     * @param from
     * @throws Exception
     */
    public void exeReStartButton(MainFrom from) throws Exception {
        String homepath = CommonUtil.getParameter("homepath");
        boolean isWindows = util.isWindows();
        String command = "";
        if(isWindows){
            command = "call "+ homepath + "\\stop.bat && call "+ homepath+"\\startup.bat";
        }else{
            command = homepath + "/stop.sh ; "+ homepath+"/startup.sh";
        }


        Logger.write("start:"+command);
        inte.exec("dos",from,command);
    }

    /***
     * 清空控制台
     * @param from
     */
    public void exeClearButton(MainFrom from){
        Logger.clearData();
    }

    /***
     * 执行检查更新动作
     * @param from
     * @throws Exception
     */
    public void exeUpdatesButton(MainFrom from) throws Exception {
        try{
            Logger.write("正在检查更新...");
            String filePath = System.getProperty("user.dir")+ "/selftool.jar";
            String md5 = CommonUtil.md5HashCode(filePath);
            String url = CommonUtil.getParameter("checkurl");

            String json = "{\"code\":\"RE_SELF_CHECK\",\"data\":{\"md5\":\""+md5+"\"}}";
            String ret = HttpConnection.doPost(url,json,null);
            Logger.write("检查结果为..."+ret);
        }catch (Exception e){
            Logger.write("很抱歉,检查更新失败,您可通过【https://gitee.com/sillage_c/selftool】与我取得联系并反馈问题，感谢您的支持！");
        }
    }
}
